<?php
namespace MagicCrypt;
date_default_timezone_set('PRC');

define("EN_GZIP",              			 true);/* 是否开启GZIP */

class MagicCrypt {
    private $iv = "0102030405060708";//密钥偏移量IV，可自定义
    private $encryptKey = "59542092AF4E4151";

    //加密
    public function encrypt($encryptStr) {
        $localIV = $this->iv;
        $encryptKey = $this->encryptKey;
        
        if (true == EN_GZIP)   $encryptStr = gzencode($encryptStr);
 
        //Open module
        $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, $localIV);
 
        mcrypt_generic_init($module, $encryptKey, $localIV);
 
        //Padding
        $block = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
        $pad = $block - (strlen($encryptStr) % $block); //Compute how many characters need to pad
        $encryptStr .= str_repeat(chr($pad), $pad); // After pad, the str length must be equal to block or its integer multiples
 
        //encrypt
        $encrypted = mcrypt_generic($module, $encryptStr);
 
        //Close
        mcrypt_generic_deinit($module);
        mcrypt_module_close($module);
 
        return $encrypted;
    }
 
    //解密
    public function decrypt($encryptStr) {
        $localIV = $this->iv;
        $encryptKey = $this->encryptKey;
 
        //Open module
        $module = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, $localIV);
 
        mcrypt_generic_init($module, $encryptKey, $localIV);
 
        $encryptedData = mdecrypt_generic($module, $encryptStr);
        
        if (true == EN_GZIP)   $encryptedData = gzdecode($encryptedData);
 
        return $encryptedData;
    }
	
	    
   public function http_requests($url, $data = null)
	{
		
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_HEADER, true);
		$header[] = "appId: EUCP-EMY-SMS1-2WQSE";
		$header[] = "gzip: on";
		curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
		
		curl_setopt($curl, CURLOPT_HEADER, true);
		if (!empty($data)){
			curl_setopt($curl, CURLOPT_POST, 1);
			curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		}
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
		$res = curl_exec($curl);
		
		$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
		curl_close($curl);
		$header = substr($res, 0, $headerSize);
		$outobj = new \stdClass();
		
		$lines = explode("\r\n",$header);
		foreach($lines as $line)
		{
			$items = explode(": ",$line);
			if(isset($items[0]) and !empty($items[0]) and 
			   isset($items[1]) and !empty($items[1]))
				$outobj->$items[0] = $items[1];
		}
		
		$outobj->ciphertext = substr($res, $headerSize);

		return $outobj;
	}

//短信发送
	
	public function SendSMSEnd($mobile, $content, $timerTime = "", $customSmsId = "",$extendedCode = "", $validPeriodtime= 120)
	{   

		// 如果您的系统环境不是UTF-8，内容需要转码到UTF-8。如下：从gb2312转到了UTF-8
		// $content = mb_convert_encoding( $content,"UTF-8","gb2312");

		$item = new \stdClass();
		$item->mobile   = $mobile;
		$item->content  = $content;
		
		/* 选填内容 */ 
		if("" != $timerTime)    $item->timerTime    = $timerTime;
		if("" != $customSmsId)  $item->customSmsId  = $customSmsId;
		if("" != $extendedCode) $item->extendedCode = $extendedCode;    
		
		$item->requestTime = self::getMillisecond();
		$item->requestValidPeriod = $validPeriodtime;
		
		$json_data = json_encode($item, JSON_UNESCAPED_UNICODE);

	   // echo "SendJson=".$json_data.END;

		//$encryptObj = new \MagicCrypt();
		//$senddata = $encryptObj->encrypt($json_data);//加密结果
		$senddata = self::encrypt($json_data);

		$url = 'shmtn.b2m.cn/inter/sendSingleSMS';
			
		$resobj = self::http_requests($url, $senddata);
		
		//$resobj->plaintext = $encryptObj->decrypt($resobj->ciphertext);
		$resobj->plaintext = self::decrypt($resobj->ciphertext);

		return $resobj;
	}
	
	public function getMillisecond() {
    list($t1, $t2) = explode(' ', microtime());
    return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
	}
	
	 /* curl 方法 */        
    public function curlGet($url=""){

		if(!$url)return 'url为空';
		$curl = curl_init();
	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	    curl_setopt($curl, CURLOPT_TIMEOUT, 500); 

	    preg_match("/^(http:\/\/|https:\/\/).*$/",$url,$match);
		if($match['1']!='https://' && $match[1]!='http://'){
			return '这不是一个链接';
		}
		//判断https加ssl验证 ,不验证ssl
		if($match[1]=='https://'){
			curl_setopt($curl, CURLOPT_FOLLOWLOCATION,1);
			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	    	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
		}
	    curl_setopt($curl, CURLOPT_URL, $url);
	    $data = curl_exec($curl);
	    // curl_error();
	    curl_close($curl);  
        return json_decode($data);
    }
}
?>
